{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`dir()` 查看 String 内置的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__getnewargs__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mod__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__rmod__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'capitalize',\n",
       " 'casefold',\n",
       " 'center',\n",
       " 'count',\n",
       " 'encode',\n",
       " 'endswith',\n",
       " 'expandtabs',\n",
       " 'find',\n",
       " 'format',\n",
       " 'format_map',\n",
       " 'index',\n",
       " 'isalnum',\n",
       " 'isalpha',\n",
       " 'isascii',\n",
       " 'isdecimal',\n",
       " 'isdigit',\n",
       " 'isidentifier',\n",
       " 'islower',\n",
       " 'isnumeric',\n",
       " 'isprintable',\n",
       " 'isspace',\n",
       " 'istitle',\n",
       " 'isupper',\n",
       " 'join',\n",
       " 'ljust',\n",
       " 'lower',\n",
       " 'lstrip',\n",
       " 'maketrans',\n",
       " 'partition',\n",
       " 'replace',\n",
       " 'rfind',\n",
       " 'rindex',\n",
       " 'rjust',\n",
       " 'rpartition',\n",
       " 'rsplit',\n",
       " 'rstrip',\n",
       " 'split',\n",
       " 'splitlines',\n",
       " 'startswith',\n",
       " 'strip',\n",
       " 'swapcase',\n",
       " 'title',\n",
       " 'translate',\n",
       " 'upper',\n",
       " 'zfill']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`len(str)` 获取字符串长度 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中文长度:  2\n",
      "英文长度:  4\n"
     ]
    }
   ],
   "source": [
    "# 中文字符串长度\n",
    "name_chinese = \"张三\"\n",
    "name_chinese_len = len(name_chinese)\n",
    "print(\"中文长度: \", name_chinese_len)\n",
    "\n",
    "\n",
    "# 英文字符长度\n",
    "name_english = \"Tony\"\n",
    "name_english_len = len(name_english)\n",
    "\n",
    "print(\"英文长度: \", name_english_len)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`str.count('string')` 统计字符串中指定字符或字符串出现的次数 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "pi_str = \"一山一寺一壶酒\"\n",
    "yi_num = pi_str.count('一')\n",
    "print(yi_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数字转字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age: 12\n"
     ]
    }
   ],
   "source": [
    "age = 12\n",
    "age_str = str(age)\n",
    "print(\"Age: \" + age_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 大小写转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "全部转换为小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'name: tony'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = \"Name: Tony\"\n",
    "user_info_lower = user_info.lower()\n",
    "print(user_info_lower)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "全部转换为大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'NAME: TONY'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = \"Name: Tony\"\n",
    "user_info_upper = user_info.upper()\n",
    "user_info_upper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "大写转小写, 小写转大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nAME: tONY'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = \"Name: Tony\"\n",
    "user_info_swapcase = user_info.swapcase()\n",
    "print(user_info_swapcase)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第一个单词首字母大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'My name is tony'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = \"my name is Tony\"\n",
    "user_info_cap = user_info.capitalize()\n",
    "print(user_info_cap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每个单词首字母大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My Name Is Tony\n"
     ]
    }
   ],
   "source": [
    "user_info = \"my name is tony\"\n",
    "user_info_title = user_info.title()\n",
    "print(user_info_title)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串填充"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- center 居中\n",
    "- ljust 左对齐\n",
    "- rjust 右对齐\n",
    "\n",
    "- 参数1: 填充之后的长度\n",
    "- 参数2: 填充使用的字母, 默认为空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**********\n",
      "    张三    \n",
      "   王大春    \n",
      "   欧阳见见   \n",
      "**********\n",
      "        张三\n",
      "       王大春\n",
      "      欧阳见见\n",
      "**********\n",
      "张三        \n",
      "王大春       \n",
      "欧阳见见      \n"
     ]
    }
   ],
   "source": [
    "# 居中对齐\n",
    "print(\"\".center(10, '*'))\n",
    "print(\"张三\".center(10))\n",
    "print(\"王大春\".center(10))\n",
    "print(\"欧阳见见\".center(10))\n",
    "\n",
    "# 右对齐\n",
    "print(\"\".center(10, '*'))\n",
    "print(\"张三\".rjust(10))\n",
    "print(\"王大春\".rjust(10))\n",
    "print(\"欧阳见见\".rjust(10))\n",
    "\n",
    "# 左对齐\n",
    "print(\"\".ljust(10, '*'))\n",
    "print(\"张三\".ljust(10))\n",
    "print(\"王大春\".ljust(10))\n",
    "print(\"欧阳见见\".ljust(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "左边用 0 填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0000000183\n"
     ]
    }
   ],
   "source": [
    "price = 183\n",
    "price_str = str(price)\n",
    "price_str_ten = price_str.zfill(10)\n",
    "print(price_str_ten)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串查找"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`find(str, start, end)`\n",
    "\n",
    "- 参数1: 带查找的字符串\n",
    "- 参数2: 查找开始位置, 默认 0\n",
    "- 参数3: 查找结束位置, 默认为字符串长度\n",
    "\n",
    "index 和 find 使用方法相同"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Index** 和 **find** 区别\n",
    "\n",
    "如果查找的字符不存在, index() 会抛出错误;\n",
    "如果查找的字符不存在, find() 会返回 -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n"
     ]
    }
   ],
   "source": [
    "pi_str = \"一山一寺一壶酒\"\n",
    "\n",
    "# index 会抛出错误\n",
    "# er_index = pi_str.index('二')\n",
    "# print(er_index)\n",
    "\n",
    "er_find_index = pi_str.find('二')\n",
    "print(er_find_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- find index 默认从左查询\n",
    "- rfind rindex 默认从右边开始查询"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串前后操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- startswith 判断字符串是否以指定的字符串开始\n",
    "- endswith 判断字符串是否以指定的字符串结束"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "pi_str = \"一山一寺一壶酒\"\n",
    "print(pi_str.startswith('一'))\n",
    "print(pi_str.endswith('二'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- strip 删除字符串 **两边** 指定的字符, 默认为 空白字符\n",
    "- lstrip 删除字符串 **左边** 指定的字符, 默认为 空白字符\n",
    "- rstrip 删除字符串 **右边** 指定的字符, 默认为 空白字符\n",
    "\n",
    "空白字符包括但不限于: 空格 Tab 换行符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "山一寺一壶酒\n"
     ]
    }
   ],
   "source": [
    "pi_str = \"一山一寺一壶酒\"\n",
    "\n",
    "# index 会抛出错误\n",
    "# er_index = pi_str.index('二')\n",
    "# print(er_index)\n",
    "\n",
    "pi_str_strip = pi_str.strip('一')\n",
    "print(pi_str_strip)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串分割"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`split(str, maxsplit)`\n",
    "\n",
    "- 参数1: 用来分割的字符, 默认使用 空白字符\n",
    "- 参数2: 分割次数, 默认全部分割\n",
    "\n",
    "rsplit 和 split 用法相同"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- split 默认从左边开始分割\n",
    "- rsplit 默认从右边开始分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三', ' 李四', ' 王五']\n"
     ]
    }
   ],
   "source": [
    "names = \"张三, 李四, 王五\"\n",
    "name_list = names.split(',')\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`splitlines([keep])` 使用换行符分割字符串\n",
    "\n",
    "- 参数1: 默认不保留换行符, 如果值为 True 则保留换行符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三', ' 李四', ' 王五']\n",
      "['张三\\n', ' 李四\\n', ' 王五']\n"
     ]
    }
   ],
   "source": [
    "names = \"张三\\n 李四\\n 王五\"\n",
    "print(names.splitlines())\n",
    "print(names.splitlines(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串拼接"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`join()` 使用字符串将列表拼接为字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三,李四,王五\n"
     ]
    }
   ],
   "source": [
    "names = [\"张三\", \"李四\", \"王五\"]\n",
    "print(\",\".join(names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 `+` 拼接字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Tony ; Age: 18\n"
     ]
    }
   ],
   "source": [
    "str_name = \"Name: Tony\"\n",
    "str_age = \"Age: 18\"\n",
    "str_info = str_name + \" ; \" + str_age\n",
    "print(str_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串内容判断"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - `isupper()` 判断是否每个字母都大写\n",
    " - `islower()` 判断是否每个字母都小写\n",
    " - `istitle()` 判断是否每个单词的首字母都大写\n",
    " - `isspace()` \n",
    " - `isalnum()`\n",
    " - `isalpha()`\n",
    " - `isascii()`\n",
    " - `isdecimal()`\n",
    " - `isdigit()`\n",
    " - `isidentifier()`\n",
    " - `isnumeric()`\n",
    " - `isprintable()`"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
